package org.dromara.system.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.bo.WbDemandListBo;
import org.dromara.system.domain.vo.WbDemandListVo;
import org.dromara.system.listener.WbDemandListImportListener;
import org.dromara.system.listener.WbGoodsListInfoImportListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbGoodsListInfoVo;
import org.dromara.system.domain.bo.WbGoodsListInfoBo;
import org.dromara.system.service.IWbGoodsListInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 货物清单
 *
 * @author Lion Li
 * @date 2024-04-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/goodsListInfo")
public class WbGoodsListInfoController extends BaseController {

    private final IWbGoodsListInfoService wbGoodsListInfoService;

    /**
     * 查询货物清单列表
     */
    @SaCheckPermission("system:goodsListInfo:list")
    @GetMapping("/list")
    public TableDataInfo<WbGoodsListInfoVo> list(WbGoodsListInfoBo bo, PageQuery pageQuery) {
        return wbGoodsListInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出货物清单列表
     */
    @SaCheckPermission("system:goodsListInfo:export")
    @Log(title = "货物清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbGoodsListInfoBo bo, HttpServletResponse response) {
        List<WbGoodsListInfoVo> list = wbGoodsListInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "货物清单", WbGoodsListInfoVo.class, response);
    }

    /**
     * 下载模板
     */
    @SaCheckPermission("system:goodsListInfo:add")
    @Log(title = "需求一览", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(WbGoodsListInfoBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "货物清单", WbGoodsListInfoVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param itemId 项目ID
     */
    @Log(title = "货物清单", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:goodsListInfo:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, String itemId) throws Exception {
        int sheetNo = 0;
        ExcelResult<WbGoodsListInfoVo> result = null;
        while (sheetNo >= 0){
            result = ExcelUtil.importExcel(file.getInputStream(),sheetNo++,WbGoodsListInfoVo.class,new WbGoodsListInfoImportListener(itemId));
            if(result.getList().size() == 0){
                sheetNo = -1;
            }
        }
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取货物清单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:goodsListInfo:query")
    @GetMapping("/{id}")
    public R<WbGoodsListInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbGoodsListInfoService.queryById(id));
    }

    /**
     * 新增货物清单
     */
    @SaCheckPermission("system:goodsListInfo:add")
    @Log(title = "货物清单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbGoodsListInfoBo bo) {
        return toAjax(wbGoodsListInfoService.insertByBo(bo));
    }

    /**
     * 修改货物清单
     */
    @SaCheckPermission("system:goodsListInfo:edit")
    @Log(title = "货物清单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbGoodsListInfoBo bo) {
        return toAjax(wbGoodsListInfoService.updateByBo(bo));
    }

    /**
     * 删除货物清单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:goodsListInfo:remove")
    @Log(title = "货物清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbGoodsListInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
